package com.lfg.leetcode.leetcode200;

public class No201rangeBitwiseAnd {

    public static void main(String[] args) {
        int ret = rangeBitwiseAnd(2, 6);

        System.out.println(ret);
    }

    public static int rangeBitwiseAnd(int m, int n) {
        int cnt = 0;
        while (m < n) {
            m >>= 1;
            n >>= 1;
            cnt++;
        }

        return m << cnt;
    }

    public static int rangeBitwiseAnd2(int m, int n) {
        int ret = m;
        for (int i = m + 1; i <= n && i > 0; i++) {
            ret = ret & i;
        }
        return ret;
    }
}
